﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.BusinessEntity;
using JM.JCY.Proxy;
using NCS.IConnect.ApplicationContexts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using System.Data;

namespace JM.JCY.SharePointComponent.Applications.ViewAllApplications
{
    public partial class ViewAllApplicationsUserControl : UserControl
    {

       
        protected void Page_Load(object sender, EventArgs e)
        {
         

            if (!IsPostBack)
            {
                using (ApplicationProxy appProxy = new ApplicationProxy())
                {
                    ApplicationDataSet.T_ApplicationDataTable appDT = appProxy.GetApplicationByType("MyApp", "Y");
                    for (int i = appDT.Rows.Count - 1; i >= 0; i--)
                    {
                        if (appDT.Rows[i]["ApplicationName"].ToString() == "修改密码")
                        {
                            appDT.Rows.RemoveAt(i);
                            appDT.AcceptChanges();
                        }
                    }
                    this.dlMyApps.DataSource = appDT;
                    this.dlMyApps.DataBind();

                    ApplicationDataSet.T_ApplicationDataTable appDT2 = appProxy.GetApplicationByType("BasicApp", null);
                    this.dlExtApps.DataSource = appDT2;
                    this.dlExtApps.DataBind();
                }
            }
        }

        protected void dlMyApps_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView drv = (DataRowView)e.Item.DataItem;
                HtmlAnchor link = (HtmlAnchor)e.Item.FindControl("linkMyApp");
                LinkButton lbMyApp = (LinkButton)e.Item.FindControl("lbMyApp");
                SetLinkURL(link, lbMyApp, drv);
            }
        }

        protected void dlExtApps_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView drv = (DataRowView)e.Item.DataItem;
                HtmlAnchor link = (HtmlAnchor)e.Item.FindControl("linkExtApp");
                LinkButton lbMyApp = (LinkButton)e.Item.FindControl("lbExtApp");

                SetLinkURLForExt(link,lbMyApp,drv,CommonFunction.IsApplicationAdmin(Int32.Parse(drv["ID"].ToString())));
            }
        }

        private void SetLinkURL(HtmlAnchor link, LinkButton linkApp, DataRowView drv)
        {
            string redirectUrl = string.Empty;
            redirectUrl = SPContext.Current.Site.ServerRelativeUrl + "" + drv["HomeUrl"].ToString();
        
            link.HRef = redirectUrl;
            linkApp.Attributes.Add("href", redirectUrl);
        }

        private void SetLinkURLForExt(HtmlAnchor link, LinkButton linkApp, DataRowView drv,bool isAdmin)
        {
            string redirectUrl = string.Empty;
          
            if(isAdmin)
                redirectUrl = SPContext.Current.Site.ServerRelativeUrl + "" + drv["HomeUrl"].ToString().Split(',')[0].ToString();
            else
                redirectUrl = SPContext.Current.Site.ServerRelativeUrl + "" + drv["HomeUrl"].ToString().Split(',')[1].ToString();

            link.HRef = redirectUrl;
            linkApp.Attributes.Add("href", redirectUrl);
        }
    }
}
